/*
 * Copyright(c) 2018-2018, YuanYue Group, Inc. All rights reserved.
 *
 * This software is the confidential and proprietary information of YuanYue Group, Inc.
 * You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with YuanYue Group.
 */
package net.wsfcp.android.qiniu;

import android.util.Log;

import com.qiniu.android.common.FixedZone;
import com.qiniu.android.http.ResponseInfo;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UpCompletionHandler;
import com.qiniu.android.storage.UploadManager;
import com.qiniu.android.storage.UploadOptions;
import com.qiniu.android.utils.StringMap;

import org.json.JSONObject;

import java.io.File;

/**
 * QiniuUtil.java是全网覆盖平台的。
 * 七牛云
 *
 * @author Shaoboyang
 * @version $Id: QiniuUtil.java, v 0.1 2018-12-26 11:07 Shaoboyang Exp $$
 */
public class QiniuUtil {
//指定zone的具体区域
//FixedZone.zone0   华东机房
//FixedZone.zone1   华北机房
//FixedZone.zone2   华南机房
//FixedZone.zoneNa0 北美机房
//自动识别上传区域
//AutoZone.autoZone
//Configuration config = new Configuration.Builder()
//.zone(Zone.autoZone)
//.build();
//UploadManager uploadManager = new UploadManager(config);

    public static QiniuUtil instance;
    public static Configuration config;
    public static UploadManager uploadManager;

    public synchronized static QiniuUtil getInstance() {

        if (instance == null) {

            instance = new QiniuUtil();
        }

        return instance;
    }

    private QiniuUtil() {

        getUploadManager();

    }


    public UploadManager getUploadManager() {
        if (config == null) {
            config = new Configuration.Builder()
//            .chunkSize(512 * 1024)        // 分片上传时，每片的大小。 默认256K
//            .putThreshhold(1024 * 1024)   // 启用分片上传阀值。默认512K
                    .connectTimeout(10)           // 链接超时。默认10秒
                    .useHttps(true)               // 是否使用https上传域名
                    .responseTimeout(60)          // 服务器响应超时。默认60秒
//            .recorder(recorder)           // recorder分片上传时，已上传片记录器。默认null
//                    .recorder(recorder, keyGen)   // keyGen 分片上传时，生成标识符，用于片记录器区分是那个文件的上传记录
                    .zone(FixedZone.zone0)        // 设置区域，指定不同区域的上传域名、备用域名、备用IP。
                    .build();
        }
// 重用uploadManager。一般地，只需要创建一个uploadManager对象
        if (uploadManager == null) {
            uploadManager = new UploadManager(config);
        }

        return uploadManager;
    }

    //上传照片
    //    data = <File对象、或 文件路径、或 字节数组>
//    String fileName = <指定七牛服务上的文件名，或 null>;
//    String imgToken = <从服务端SDK获取>;
    public void upLoadImg(File data, String fileName, String imgToken) {

        uploadManager.put(data, fileName, imgToken, new UpCompletionHandler() {
            @Override
            public void complete(String key, ResponseInfo info, JSONObject res) {
                //res包含hash、key等信息，具体字段取决于上传策略的设置

                if (info.isOK()) {
                    Log.i("qiniu", "Upload Success");
                } else {
                    Log.i("qiniu", "Upload Fail");
                    //如果失败，这里可以把info信息上报自己的服务器，便于后面分析上传错误原因
                }

                Log.e("qiniu_result", res.toString());

                // Log.i("qiniu", key +",\r\n "+ info +",\r\n "+ res);
            }


        }, null);


    }




}